clear
set more      off
set varabbrev off
set gr        off
set linesize 255

/* INPUTS */

*cd "/data2/cedsei/data2"

********************************************************************************

foreach num of numlist 2005(1)2018 {
	clear 
	use lopnr sun2000niva seninvar year using /data/rawdata/cleaned/lisa_ind_2006
	if `num'>2005 {
		append using akulisa 
	}
	bys lopnr (year): keep if _n==_N
	if `num'==2018 {
		drop year 
	}
	save akulisa, replace
}

clear 
use searchaku 

foreach var of varlist search_afcoach search_afself search_databa search_recruit search_direct search_postads search_readads search_friends search_interview search_int {
	replace `var' = 0 if mi(`var')	
}
gen search_ext = search_int>0
gen search_ind = max(search_databa, search_direct, search_postads, search_recruit)
gen search_ind2 = max(search_databa, search_direct, search_postads, search_recruit, search_friends, search_readads)
tab search_ext search

{ // New job using hu11a
destring hu11b, replace 
gen datejobb  = ym(hu11a,hu11b)
format datejobb %tm
order lopnr date datejobb
foreach num of numlist 1 2 {
	bys lopnr (date): gen datejobb_f`num' = datejobb[_n+`num']
	format datejobb_f`num' %tm
}
gen temp = ym(year(date),month(date))
format temp %tm 
gen newjob_hu1 = cond(!mi(datejobb_f1),cond(datejobb_f1>=temp,1,0),0)
gen newjob_hu2 = newjob_hu1 
replace newjob_hu2 = 1 if newjob_hu2 == 0 & datejobb_f2!=. & datejobb_f2>=temp 
bys lopnr (date): replace newjob_hu1 = . if _n==_N
bys lopnr (date): replace newjob_hu2 = . if _n==_N
bys lopnr (date): replace newjob_hu2 = . if _n==_N-1
drop temp
count if mi(newjob_hu1) 
}

{ // New job using externrekryterins-questions.
gen temp = cond(exb1!=.,cond(inlist(exb1,1,2,3),1,0),0)
// gen temp = cond(exb1!=.,1,0)
foreach num of numlist 1 2 {
	bys lopnr (date): gen nj_f`num' = temp[_n+`num']
}
drop temp 
gen newjob_ex1 = cond(!mi(nj_f1),cond(nj_f1==1,1,0),.)
gen newjob_ex2 = newjob_ex1 
replace newjob_ex2 = 1 if newjob_ex2 == 0 & newjob_ex2!=. & nj_f2!=. & nj_f2==1
drop nj*
}

gen emp = cond(inlist(arbstatuslankad,2,3),1,cond(inlist(arbstatuslankad,1,4),0,99))
drop if emp==99 

gen t1= 1 
egen t2 = total(t1), by(lopnr)
keep if t2>1
drop t1 t2 
replace search_int = 8 if search_int>8 // winsorization
gen year = year(date)
gen month = month(date)

merge m:1 lopnr using "/data/rawdata/cleaned/yearofbirth"
drop if _merge==2
drop _merge
merge m:1 lopnr using "/data/rawdata/cleaned/gender"
drop if _merge==2
drop _merge
merge m:1 lopnr using "akulisa"
drop if _merge==2
drop _merge

gen immi = cond(seninvar!="0000" & seninvar!="",1,0)
gen edu = substr(sun2000niva,1,1)
destring edu, replace 
replace edu = 11 if mi(edu)

save "$datapath/A1_aku_prep.dta", replace 
